

***creation of Figure 1 for Hyde 2020 Science Review Essay on Democratic Backsliding & International Environment
***Nelda data merged with Gleditsch and Ward-based balanced panel data
clear

use "neldapanelMay2020.dta" 

**generate binary indicator of whether the country held an election in the calendar year and a 5 year moving window of elections held in a given country in year t, t-1, t-2, t-3, or t-4
replace election=0 if election==.
generate election5=1 if election==1 | L1.election==1 | L2.election==1 | L3.election==1 | L4.election==1 
replace election5=0 if election5==.

**generate indicator of whether those elections allowed for the possibility of competition

generate competition=0
replace competition=1 if nelda3==1 & nelda4==1 & nelda5==1
generate competition5year=0
replace competition5year=1 if competition==1 | L1.competition==1 | L2.competition==1 | L3.competition==1 | L4.competition==1 

**Generate variable for opposition harassment and/or media bias
generate electionprob=0
replace electionprob=1 if nelda15==1 | nelda16==1 


**generate counter for collapsing data annual summary totals of relevant variables
generate state=1

collapse (sum) state  election5 election electionprob competition5year, by(year)

**generate annual rates of elections, five year moving window of elections, and competition
generate rateelection5=(election5/state)*100
generate rateelection=(election/state)*100
generate ratecompetition5=(competition5/state)*100

*Figure 1
twoway line  rateelection5 ratecompetition5 year if year>1949 & year<2018, yscale(range(0 1)) ylabel(0 (10) 100) ytitle("Rate of all Sovereign States") xlabel(1950(10) 2018) legend(label(1 "Recent National Elections (5 year moving window)") label(2 "Recent Potentially Competitive Elections (5 year moving window)")) legend(cols(1))

*Figure 3
twoway line  election year if year<2019 || area electionprob year if year<2019 ||lowess election year if year<2019 ||lowess electionprob year if year<2019,   ytitle("Total Number Per Year") xlabel(1945(5) 2015) ylabel(0 (10) 70) legend(label(1 "Total Elections per Year") label (2 "Total Elections with Government Harassment of Opposition" "and/or Media Bias in Favor of the Incumbent" ) label(3 "Smoothed Trend Line (lowess)") label(4 "Smoothed Trend Line (lowess)")) legend(cols(1))
